package com.fdb.efp.console.api.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.console.common.ConsoleConstant;
import com.fdb.efp.console.service.facade.PrdInfoService;
import com.fdb.efp.console.service.vo.PrdInfoVO;
import com.fdb.efp.console.common.constant.RuleTypeConstant;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.efp.console.service.facade.SRuleCollService;
import com.fdb.efp.console.service.vo.SRuleCollVO;
import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class SRuleCollController extends AbstractController  {
	
	@Autowired
	@Qualifier("sRuleCollService")
	private SRuleCollService sRuleCollService;
	@Autowired
	@Qualifier("prdInfoService")
	private PrdInfoService prdInfoService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/rule/colls", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleCollVO>> querySRuleCollAll(@RequestBody SRuleCollVO vo) {		
		setUserInfoToVO(vo);
		// 根据用户角色获取用户权限方案
		List<SRuleCollVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sRuleCollService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sRuleCollService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sRuleCollService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sRuleCollService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/{ruleCollId}", method=RequestMethod.POST)
	public @ResponseBody ResponseData<SRuleCollVO> queryByPk(@PathVariable("ruleCollId") String ruleCollId) {
		//查询是否有数据权限
		Map<String, String> key = new HashMap<>();
		key.put("ruleCollId", ruleCollId);

		boolean checkRLT = this.checkOptionalAuthority(key, ConsoleConstant.URL_DATA_PERMISS_RULE_COLL, this.sRuleCollService);
		if(!checkRLT)
			return ResponseData.create(null, ApplicationConstance.NO_AUTHORITY, ApplicationConstance.NO_AUTHORITY_MESSAGE, false);
		SRuleCollVO inVo = new SRuleCollVO();
				inVo.setRuleCollId(ruleCollId);
		SRuleCollVO outputVo = sRuleCollService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param inSRuleCollVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRuleCollVO inSRuleCollVo) {
		setUserInfoToVO(inSRuleCollVo);
		int outputVo = sRuleCollService.deleteByPk(inSRuleCollVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSRuleCollVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRuleCollVO inSRuleCollVo) {
		setUserInfoToVO(inSRuleCollVo);
		int outputVo = sRuleCollService.updateByPk(inSRuleCollVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSRuleCollVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRuleColl(@RequestBody SRuleCollVO inSRuleCollVo) {
		inSRuleCollVo = setUserInfoToVO(inSRuleCollVo);
		int outputVo = sRuleCollService.insertSRuleColl(inSRuleCollVo);
		if (outputVo < 0 ) {
			String message = "处理失败!";
			return getFailuerMsg(message, outputVo);
		}else
			return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据,并且返回这个对象
	 */
	@RequestMapping(value="/s/rule/coll/back", method=RequestMethod.POST)
	public @ResponseBody ResponseData<SRuleCollVO> insertSRuleCollBack(@RequestBody SRuleCollVO inSRuleCollVo) {
		inSRuleCollVo = setUserInfoToVO(inSRuleCollVo);
		inSRuleCollVo = sRuleCollService.insertSRuleCollBack(inSRuleCollVo);
		int outputVo = inSRuleCollVo.getTotal();
		if (outputVo < 0 ) {
			String message;
			switch (outputVo) {
				default:
					message = "处理失败!";
					break;
			}
			ResponseData<SRuleCollVO> responseData = new ResponseData<SRuleCollVO>();
			responseData.setRows(inSRuleCollVo);
			responseData.setMessage(message);
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}
			
		return getResponseData(inSRuleCollVo);
	}
	/**
	 * 提交规则集
	 * @param inSRuleCollVo
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/submit", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> submitSRuleColl(@RequestBody SRuleCollVO inSRuleCollVo) {
		inSRuleCollVo = setUserInfoToVO(inSRuleCollVo);
		int outputVo = sRuleCollService.submitSRuleColl(inSRuleCollVo);
		
		if (outputVo < 0 ) {
			String message;
			switch (outputVo) {
				case -3:
					message = "当前规则类型已存在生效的规则集!";
					break;
				case -4:
					message = "存在相关参数配置未完成!";
					break;
				case -5:
					message = "该规则集下没有子规则不允许提交生效!";
					break;
				default:
					message = "处理失败!";
					break;
			}
			return getFailuerMsg(message, outputVo);
		}
		return getResponseData(outputVo);
	}
	/**
	 * 失效规则
	 * @param inSRuleCollVo
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/disable", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> disableSRuleColl(@RequestBody SRuleCollVO inSRuleCollVo) {
		inSRuleCollVo = setUserInfoToVO(inSRuleCollVo);
		//查询这个规则集ID 被那些产品ID引用
		List<String> quotePrdInfos = prdInfoService.queryRuleCollQuotePrdId(inSRuleCollVo.getRuleCollId());
		if (quotePrdInfos == null || quotePrdInfos.size() == 0){
			SRuleCollVO ruleColl = sRuleCollService.queryByPk(inSRuleCollVo);
			if (RuleTypeConstant.CREDIT_CALC.equals(ruleColl.getRuleCollType())) {
				PrdInfoVO prdInfoVo = new PrdInfoVO();
				//crdMeaMedolId 额度测算模型ID
				prdInfoVo.setCrdMeaMedolId(inSRuleCollVo.getRuleCollId());
				List<PrdInfoVO> prdInfoVOs = prdInfoService.queryPrdInfoByOthers(prdInfoVo);
				if ( null != prdInfoVOs && prdInfoVOs.size() > 0 ) {
					quotePrdInfos = new ArrayList<>(prdInfoVOs.size());
					for (PrdInfoVO temp : prdInfoVOs) {
						quotePrdInfos.add(temp.getPrdId());
					}
				}
			}
		}
		//如果存在规则集ID 被生效的产品引用，则不能失效
		if (quotePrdInfos != null && quotePrdInfos.size() > 0) {
			StringBuilder message = new StringBuilder(inSRuleCollVo.getRuleCollId());
			message.append("被生效的产品ID集合")
					.append(quotePrdInfos)
					.append("引用");
			return getFailuerMsg(message.toString(), -1);
		}

		int outputVo = sRuleCollService.disableSRuleColl(inSRuleCollVo);
		return getResponseData(outputVo);
	}
	/**
	 * 复制规则集
	 * @param inSRuleCollVo
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/copy", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> copySRuleColl(@RequestBody SRuleCollVO inSRuleCollVo) {
		inSRuleCollVo = setUserInfoToVO(inSRuleCollVo);
		int outputVo = sRuleCollService.copyRuleCollById(inSRuleCollVo);
		return getResponseData(outputVo);
	}
	
	private ResponseData<Integer> getFailuerMsg(String message, Integer outputVo){
		ResponseData<Integer> responseData = new ResponseData<Integer>();
		responseData.setRows(outputVo);
		responseData.setMessage(message);
		responseData.setCode(ApplicationConstance.FAILURE);
		responseData.setSuccess(false);
		return responseData;
	}
}
